package com.mcptt.db.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.db.DbConst;

public class HistoryInfo
{
    //	private final static int ServerType_Message = 0;
    //	private final static int ServerType_Call = 1;
    public static String THIS_FILE = "HistoryInfo";
    private AddrInfo receiver = null;
    private AddrInfo lastSender = null;
    private int lastMsgType = SessionInfo.Msgtype_Affiliation;//消息类型
    private boolean isAffiliation = false;//消息类型




    private String lastMsgContent;//消息内容（包含发送用户名名）
    private String lastMsgDate;   //接受时间
    private int unreadCount = 0;  //消息个数



    public HistoryInfo(AddrInfo receiver)
    {
    	this.receiver = receiver;
    }

    public HistoryInfo(Cursor c) 
    {
	receiver = new AddrInfo();
	receiver.setType(UserInfo.UserType_User);

	lastSender = new AddrInfo();
	lastSender.setType(UserInfo.UserType_User);

	ContentValues args = new ContentValues();
	DatabaseUtils.cursorRowToContentValues(c, args);
	createFromContentValue(args);
    }

    public final void createFromContentValue(ContentValues args) 
    {

	String tmp_s;

	//		tmp_s = args.getAsString(DbConst.COL_ReceiverUId);
	//		if(tmp_s != null) {
	//			receiver.setuId(tmp_s);
	//		}
	tmp_s = args.getAsString(DbConst.COL_ReceiverNum);
	if(tmp_s != null) {
	    receiver.setMcId(tmp_s);
	}
	
	tmp_s = args.getAsString(DbConst.COL_Type);
	if(tmp_s != null) 
	{
	    int value = CheckData.parseInt(tmp_s);
	    if(value != -1)
	    {
		receiver.setType(value);
	    }
	}
	tmp_s = args.getAsString(DbConst.COL_Name);
	if(tmp_s != null) {
	    receiver.setName(tmp_s);
	}

	//		tmp_s = args.getAsString(DbConst.COL_lastSenderUId);
	//		if(tmp_s != null) {
	//			lastSender.setuId(tmp_s);
	//		}
	tmp_s = args.getAsString(DbConst.COL_lastSenderNum);
	if(tmp_s != null) {
	    lastSender.setMcId(tmp_s);
	}

	tmp_s = args.getAsString(DbConst.COL_lastSenderName);
	if(tmp_s != null) {
	    lastSender.setName(tmp_s);
	}

	tmp_s = args.getAsString(DbConst.COL_LastMsgType);
	if(tmp_s != null) 
	{
	    int value = CheckData.parseInt(tmp_s);
	    if(value != -1)
	    {
		lastMsgType = value;
	    }
	    else
	    {
		lastMsgType = SessionInfo.Msgtype_Affiliation;
	    }
	}
	
	tmp_s = args.getAsString(DbConst.COL_LastMsgContent);
	if(tmp_s != null) {
	    lastMsgContent = tmp_s;
	}
	tmp_s = args.getAsString(DbConst.COL_LastMsgDate);
	if(tmp_s != null) {
	    lastMsgDate = tmp_s;
	}
	tmp_s = args.getAsString(DbConst.COL_UnreadCount);
	if(tmp_s != null) 
	{
	    int value = CheckData.parseInt(tmp_s);
	    if(value != -1)
	    {
		unreadCount = value;
	    }
	}
	tmp_s = args.getAsString(DbConst.COL_Affiliation);
	if(tmp_s != null) 
	{
	    int value = CheckData.parseInt(tmp_s);
	    if(value == 1)
	    {
		isAffiliation = true;
	    }
	    else
	    {
		isAffiliation = false;
	    }
	}
	
    }

    /**
     * Pack the object content value to store
     * 
     * @return The content value representing the message
     */
    public ContentValues getContentValues() {

	ContentValues cv = new ContentValues();
	//		cv.put(DbConst.COL_ReceiverUId, receiver.getuId());
	cv.put(DbConst.COL_ReceiverNum, receiver.getMcId());
	//		cv.put(DbConst.COL_lastSenderUId, lastSender.getuId());
	cv.put(DbConst.COL_lastSenderNum, lastSender.getMcId());
	cv.put(DbConst.COL_lastSenderName, lastSender.getName());
	cv.put(DbConst.COL_LastMsgType, lastMsgType);
	cv.put(DbConst.COL_LastMsgContent, lastMsgContent);
	cv.put(DbConst.COL_LastMsgDate, lastMsgDate);
	cv.put(DbConst.COL_UnreadCount, unreadCount);
	if(isAffiliation)
	{
	    cv.put(DbConst.COL_Affiliation, "1");
	}
	else
	{
	    cv.put(DbConst.COL_Affiliation, "0");
	}
	return cv;
    }

    public AddrInfo getReceiver() {
	return receiver;
    }
    public AddrInfo getLastSender() {
	return lastSender;
    }
    public void setLastSender(AddrInfo lastSender) {
	this.lastSender = lastSender;
    }
    public String getLastMsgContent() {
	return lastMsgContent;
    }

    public void setLastMsgContent(String lastMsgContent) {
	this.lastMsgContent = lastMsgContent;
    }

    public String getLastMsgDate() {
	return lastMsgDate;
    }

    public void setLastMsgDate(String lastMsgDate) {
	this.lastMsgDate = lastMsgDate;
    }

    public String getDscp()
    {
    	return receiver.getDscp();
    }

    public void setUnreadCount(int unreadCount) {
	this.unreadCount = unreadCount;
    }
    public int getUnreadCount() {
	return unreadCount;
    }
    
    public int getLastMsgType()
    {
        return lastMsgType;
    }

    public void setLastMsgType(int lastMsgType)
    {
        this.lastMsgType = lastMsgType;
        if(lastMsgType == SessionInfo.Msgtype_Affiliation)
        {
            isAffiliation = true;
        }
        else
        {
            isAffiliation = false;
        }
    }
    
    public boolean isAffiliation()
    {
        return isAffiliation;
    }

    public String toString()
    {
	StringBuffer buffer = new StringBuffer("HistoryInfo:");
	buffer.append("receiver = ").append(receiver).append(",");
	buffer.append("lastSender = ").append(lastSender).append(",");
	buffer.append("lastMsgType = ").append(lastMsgType).append(",");
	buffer.append("lastMsgContent = ").append(lastMsgContent).append(",");
	buffer.append("lastMsgDate = ").append(lastMsgDate).append(",");
	buffer.append("unreadCount = ").append(unreadCount).append(",");
	buffer.append("isAffiliation = ").append(isAffiliation).append(";");

	return buffer.toString();
    }
}
